package cn.toutatis.sandbox.root.security.service.controller;


import cn.toutatis.common.config.ResultCode;
import cn.toutatis.common.controllerDepand.BaseControllerImpl;
import cn.toutatis.common.controllerDepand.Polymerization;
import cn.toutatis.common.log.LogBackHandle;
import cn.toutatis.common.result.Actions;
import cn.toutatis.common.result.Result;
import cn.toutatis.sandbox.root.security.service.entity.SystemUser;
import cn.toutatis.sandbox.root.security.service.service.SystemUserService;
import cn.toutatis.sandbox.root.systemService.service.SystemDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

/**
 * <p>
 * 必要用户表 前端控制器
 * </p>
 *
 * @author Toutatis_Gc
 * @since 2020-08-13
 */
@Polymerization
@LogBackHandle("SystemUserController 必要用户表")
@RequestMapping("/api/systemUser")
public class SystemUserController extends BaseControllerImpl<SystemUser, SystemUserService> {

    @Autowired
    SystemDictionaryService dictionaryService;

    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView("root/system/setting/SystemUserIndex");
        modelAndView.addObject("status",dictionaryService.staticDic("SYSTEM_STATUS_0000"));
        return modelAndView;
    }

    /*   自定义后台方法区域   */

    @Override
    @RequestMapping(value = "/saveRecord",method = RequestMethod.POST)
    public Result saveRecord(SystemUser entity) {
        if (entity == null){
            result.setMessage(ResultCode.NOT_HAVE_SELECT_DATA);
            return result;
        }

        String secret = entity.getSecret();
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode(secret);
        entity.setSecret(encode);
        if (entity.getCreateTime() == null){
            entity.setCreateTime(new Date());
        }
        if (entity.getLastUpdateTime() == null){
            entity.setLastUpdateTime(new Date());
        }

        if (entity.getStatus() == null){
            String normalSign = globalMap == null ? "SYS_OPEN_0000" : (String) globalMap.get("normalSign");
            entity.setStatus(normalSign);
        }
        boolean save = service.save(entity);
        result.setSign(save, Actions.INSERT);
        return result;
    }

    /*   对外开放接口方法区域   */


}